/**
 * 模块管理
 * 管理模块的
 *  加载
 *  初始化
 *  析构
 *  管理模块的数据
 */
var logManager=require("./Logs.js");
module.exports=Class.extend({
    path:"",
    moduleList:{},
    urlDic:{},
    eventDic:{},
    eventManager:null,
    httpManager:null,
    init:function(eventManager,httpManager){
        this.eventManager=eventManager;
        this.httpManager=httpManager;
    },
    setPath:function(path){
        this.path=path;
        logManager.logM("moduleManager init:"+path);
    },
    updateModule(path){
        var res=this.removeModule(path);
        res=this.loadModule(path);
        return res;
    },
    deleteRequireCache:function(absPath){
        if(absPath.indexOf("node_modules")!=-1)return 0;
        var count=0;
        var self=this;
        absPath=absPath.replace(/\//g,"\\")
        absPath=absPath.replace(/\\\\/g,"\\")
        var req=require.cache[absPath];
        if(!req) {
            absPath=absPath.replace(/\\/g,"/")
            req = require.cache[absPath]
        }
        if(req&&req.children){
            for(var key in req.children){
                var temReq=req.children[key];
                count+=self.deleteRequireCache(temReq.id);
            }
        }
        logManager.logM("delete:"+absPath);
        delete require.cache[absPath];
        count++;
        return count;
    },
    removeModule(path){
        if(!this.moduleList[path])return;
        logManager.logM("移除模块："+path)
        var tmodule=this.moduleList[path];
        try{
            if(tmodule.dispose)
                tmodule.dispose();
        }catch(e){
            logManager.logE(e)
        }
        this.removeEvent(path);
        delete this.moduleList[path];
        delete this.eventDic[path];
        delete this.urlDic[path];

        //移除所有与之相关的require
        var absPath=this.path+path;
        var num=this.deleteRequireCache(absPath);
        logManager.logM("remove js file num:"+num);
        return true;
    },
    loadModule(path){
        var self=this;
        var tmodule=self.moduleList[path];
        if(tmodule) {
            logManager.logW("module is already loaded:"+path);
            return false;
        }
        logManager.logM("加载模块："+path)
        try{
            tmodule=require(self.path+path);
            self.moduleList[path]=tmodule;
            self.urlDic[path]=[];
            self.eventDic[path]=[];
            if(tmodule.init)
                tmodule.init();
            if(tmodule["urlListen"]&&tmodule["urlListen"].length){
                for(var i=0;i<tmodule["urlListen"].length;i++) {
                    var data=tmodule["urlListen"][i];
                    self.httpManager.addUrlListen(data.key, data.fun, data.thisObj?data.thisObj:data);
                    self.urlDic[path].push([data.key, data.fun, data.thisObj?data.thisObj:data]);
                }
            }
            if(tmodule["eventListen"]){
                for(var i=0;i<tmodule["eventListen"].length;i++) {
                    var data = tmodule["eventListen"][i];
                    self.eventManager.addListener(data.key, data.fun, data.thisObj ? data.thisObj : data)
                    self.eventDic[path].push([data.key, data.fun, data.thisObj ? data.thisObj : data]);
                }
            }
        }catch(e){
            self.removeEvent(path);
            logManager.logE("load module error:"+path)
            logManager.logE(e.stack)
            return false;
        }
        return true;
    },
    removeEvent:function(path){
        var list=this.urlDic[path];
        if(list){
            for(var i=0;i<list.length;i++){
                this.httpManager.removeUrlListen(list[i][0], list[i][1], list[i][2]);
            }
        }
        list=this.eventDic[path];
        if(list){
            for(var i=0;i<list.length;i++){
                this.eventManager.removeListener(list[i][0], list[i][1], list[i][2]);
            }
        }
    }
})